/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.shusongxian.service.impl;

import me.zhengjie.suli.shusongxian.domain.AgvShusongxian;
import me.zhengjie.suli.shusongxian.service.AgvShusongxianService;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.suli.shusongxian.repository.AgvShusongxianRepository;
import me.zhengjie.suli.shusongxian.service.dto.AgvShusongxianDto;
import me.zhengjie.suli.shusongxian.service.dto.AgvShusongxianQueryCriteria;
import me.zhengjie.suli.shusongxian.service.mapstruct.AgvShusongxianMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author WYH
* @date 2024-03-27
**/
@Service
@RequiredArgsConstructor
public class AgvShusongxianServiceImpl implements AgvShusongxianService {

    private final AgvShusongxianRepository agvShusongxianRepository;
    private final AgvShusongxianMapper agvShusongxianMapper;

    @Override
    public Map<String,Object> queryAll(AgvShusongxianQueryCriteria criteria, Pageable pageable){
        Page<AgvShusongxian> page = agvShusongxianRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(agvShusongxianMapper::toDto));
    }

    @Override
    public List<AgvShusongxianDto> queryAll(AgvShusongxianQueryCriteria criteria){
        return agvShusongxianMapper.toDto(agvShusongxianRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AgvShusongxianDto findById(Integer id) {
        AgvShusongxian agvShusongxian = agvShusongxianRepository.findById(id).orElseGet(AgvShusongxian::new);
        ValidationUtil.isNull(agvShusongxian.getId(),"AgvShusongxian","id",id);
        return agvShusongxianMapper.toDto(agvShusongxian);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AgvShusongxianDto create(AgvShusongxian resources) {
        return agvShusongxianMapper.toDto(agvShusongxianRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AgvShusongxian resources) {
        AgvShusongxian agvShusongxian = agvShusongxianRepository.findById(resources.getId()).orElseGet(AgvShusongxian::new);
        ValidationUtil.isNull( agvShusongxian.getId(),"AgvShusongxian","id",resources.getId());
        agvShusongxian.copy(resources);
        agvShusongxianRepository.save(agvShusongxian);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            agvShusongxianRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AgvShusongxianDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AgvShusongxianDto agvShusongxian : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("输送线号", agvShusongxian.getShusongxian());
            map.put("入空箱数量", agvShusongxian.getNumber());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}